package com.ztgf.shopping.service.shop;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.ztgf.shopping.models.dto.shop.ShopInfoDTO;
import com.ztgf.shopping.models.dto.shop.ShopQualificationDTO;
import com.ztgf.shopping.models.service.shop.IShopInfoService;
import com.ztgf.shopping.models.service.shop.IShopQualificationService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import javax.annotation.Resource;
import com.ztgf.shopping.common.config.YmlConfig;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.shopping.mapper.shop.ShopAuditLogMapper;
import com.ztgf.shopping.models.service.shop.IShopAuditLogService;
import com.ztgf.shopping.models.bo.shop.ShopAuditLogSaveBO;
import com.ztgf.shopping.models.dto.shop.ShopAuditLogDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;

import java.util.ArrayList;
import java.util.List;

/**
 * 店铺资质审核表控制器
 *
 * @author zhanghualang
 * @Date 2020-05-28 14:41:26
 */
@DubboService
@Lazy
public class ShopAuditLogServiceImpl extends ServiceImpl<ShopAuditLogMapper, ShopAuditLogDTO> implements IShopAuditLogService{
    @Autowired
    private IShopInfoService iShopInfoService;

    @Autowired
    private IShopQualificationService iShopQualificationService;

    @Resource
    private YmlConfig ymlConfig;

    @Override
    public boolean add(ShopAuditLogSaveBO saveBO) {
//        ShopAuditLogDTO dto = new ShopAuditLogDTO();
//        BeanUtils.copyProperties(saveBO,dto);
//        Long id = ymlConfig.getPkId();
//        dto.setId(id);
//        this.insert(dto);
//
//        /**
//         * 同步更新资质表审核状态
//         */
//        ShopQualificationDTO shopQualificationDTO = iShopQualificationService.selectById(saveBO.getShopQualificationId());
//        //审核日志表的0打回、1审核通过、2审核失败  资质表的0未审核、1打回、2审核通过、3审核失败
//        shopQualificationDTO.setAuditStatus(saveBO.getAuditStatus() + 1);
//        iShopQualificationService.updateById(shopQualificationDTO);
//
//        /**
//         * 判断店铺是否通过审核
//         */
//        ShopInfoDTO shopInfoDTO = iShopInfoService.selectById(saveBO.getShopId());
//
//        if (saveBO.getAuditStatus() == 0 || saveBO.getAuditStatus() == 2){
//            //审核失败
//            //注册买家状态 0创建、1审核通过、2审核不通过、3冻结
//            //注册卖家状态 0审核中、1审核通过、2审核不通过、3入住合同已签订、4保证金已缴纳、5年费已缴纳、6冻结7d、7冻结14D、8冻结30天、9冻结永久
//            if (shopInfoDTO.getMerchantType() == 0){//0买家，1卖家
//                shopInfoDTO.setBuyerStatus(2);
//            }else {
//                //0营业执照，1法人身份证，3开户许可证，4商标注册证书，5续展证明文件，6ISO认证书，7品牌授权书
//                if (saveBO.getType() < 3 ){
//                    //若是基础资质
//                    shopInfoDTO.setSellerStatus(2);
//                }
//
//                if (shopInfoDTO.getSellerStatus() == 0 || shopInfoDTO.getSellerStatus() == 2){
//                    //若是开店审核
//                    shopInfoDTO.setSellerStatus(2);
//                }
//            }
//            return iShopInfoService.updateById(shopInfoDTO);
//        }else {
//            //循环检测是否有未审核，审核失败的
//            Wrapper<ShopQualificationDTO> wrapper = new EntityWrapper<>();
//            wrapper.eq("shop_id", saveBO.getShopId());
//            List<ShopQualificationDTO> shopQualificationDTOList = iShopQualificationService.selectList(wrapper);
//
//            boolean status = true;
//            for (ShopQualificationDTO sqdto : shopQualificationDTOList){
//                //0未审核、1打回、2审核通过、3审核失败
//                if (sqdto.getAuditStatus() != 2){
//                    status = false;
//                }
//            }
//
//            if (status){
//                if (shopInfoDTO.getMerchantType() == 0){//0买家，1卖家
//                    shopInfoDTO.setBuyerStatus(1);
//                }else {
//                    if (shopInfoDTO.getSellerStatus() == 0 || shopInfoDTO.getSellerStatus() == 2){
//                        //若是开店审核
//                        shopInfoDTO.setSellerStatus(1);
//                    }
//                }
//                return iShopInfoService.updateById(shopInfoDTO);
//            }
//            return true;
//        }
        return true;
    }
}
